<?php

require 'application/modele/commande.php';
$message = '';
if (isset($_POST['a'])) {
    $action = $_POST['a'];
    if ($action == 'vider') {
        $flash['type'] = "infos";
        $flash['message'] = "Panier vidé";
    } else {
        if (count($_SESSION['panier']) != 0) {
            $commande = new Commande($mdb2);
            $commande->ajouter($_SESSION['id'], $_SESSION['panier']);
            $flash['type'] = "success";
            $flash['message'] = "Commande enregistrée";
        } else {
            $flash['type'] = "infos";
            $flash['message'] = "Aucun livre dans le panier";
        }
    }
    $_SESSION['panier'] = array();
}
$livres = $_SESSION['panier'];
$cout = coutTotal($livres);
$smarty->assign('livres', $livres);
$smarty->assign('cout', $cout);
$smarty->assign("flash", $flash);

$smarty->display("panier.tpl");

function coutTotal($livres) {
    $cout = 0.00;
    foreach ($livres as $livre) {
        $cout += floatval($livre['prix']);
    }
    return $cout;
}

?>
